﻿using AutoMapper;
using Common.MyCustomException;
using Common.Result;
using IService;
using Model.Dto.ShoppingCart;
using Model.Entitys;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public class ShoppingCartService : IShoppingCartService
    {
        private readonly ISqlSugarClient _db;
        private readonly IMapper _mapper;
        public ShoppingCartService(ISqlSugarClient db,IMapper mapper)
        {
            _db = db;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加购物车、修改购物车数量
        /// </summary>
        /// <param name="shoppingCartAdd"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> AddOrEditAsync(ShoppingCartAdd shoppingCartAdd, long userId,bool sub)
        {
            //添加到购物车的是菜品或者是套餐
            var dbShoppingCart = await _db.Queryable<ShoppingCart>()
                .Where(it=>it.UserId == userId)
                .WhereIF(shoppingCartAdd.DishId!=null,it=>it.DishId== shoppingCartAdd.DishId)
                .WhereIF(shoppingCartAdd.SetmealId!=null,it=>it.SetmealId==shoppingCartAdd.SetmealId)
                .FirstAsync();
            if (dbShoppingCart != null)
            {
                //购物车减少数量
                //sub为true则为减少数量
                if (sub)
                {
                    dbShoppingCart.Number--;
                    //数量为0,删除
                    if (dbShoppingCart.Number <= 0)
                    {
                        await _db.Deleteable<ShoppingCart>(dbShoppingCart).ExecuteCommandAsync();
                    }
                }
                else
                {
                    //购物车数量加1
                    dbShoppingCart.Number++;
                }
                await _db.Updateable(dbShoppingCart).ExecuteCommandAsync();
            }
            else
            {
                //如果不存在，则添加到购物车，数量默认就是一
                var shoppingCart = _mapper.Map<ShoppingCart>(shoppingCartAdd);
                //设置用户id，指定当前是哪个用户的购物车数据
                shoppingCart.UserId = userId;
                shoppingCart.Number = 1;
                await _db.Insertable(shoppingCart).ExecuteReturnSnowflakeIdAsync();
            }
            return true;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        /// <exception cref="MyCustomException"></exception>
        public async Task<bool> CleanAsync(long userId)
        {
            var col= await _db.Deleteable<ShoppingCart>().Where(it => it.UserId == userId).ExecuteCommandAsync();
            if(col>0) return true;
            throw new MyCustomException("删除购物车失败");
        }

        /// <summary>
        /// 查看购物车
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<List<ShoppingCart>> GetShoppingCartsAsync(long userId)
        {
            return await _db.Queryable<ShoppingCart>()
                .Where(it=>it.UserId==userId)
                .OrderBy(it=>it.CreateTime).ToListAsync();
        }

    }
}
